package cxm.proj.xtalk;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import cxm.lib.alg.RingBuffer;

public class TestActivity extends Activity {
	RingBuffer ringBuffer = null;
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);  
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		this.ringBuffer = new RingBuffer(20);
		
		short ashort = (short)-32758;
		
        byte lsb = (byte)(ashort >> 0);
        byte msb = (byte)(ashort >> 8);
        
        ashort = (short)((msb << 8) | (lsb & 0xff));
        // ashort = msb;
        
        this.setTitle("value " + ashort);
	}

	public void onPutButtonClicked(View view) {
		byte []buffer = new byte[10];
		for (int i = 0; i < 10; i++)
			buffer[i] = (byte)i;
		int putLength = this.ringBuffer.putBuffer(buffer);
		
		Button button = (Button)view;
		button.setText("Put buffer " + putLength);
	}
	
	public void onGetButtonClicked(View view) {
		Button button = (Button)view;
		
		int getLength = 7;
		byte []buffer = new byte[getLength];
		int getCount = this.ringBuffer.getBuffer(buffer);
		if (getCount == 0) {
			button.setText("Empty");
		} else {
			String text = "Get text count " + getCount + ": ";
			for (int i = 0; i < getCount; i++) {
				text += i;
				text += " - ";
				text += buffer[i] + ", ";
			}
			button.setText(text);
		}
	}
}
